package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Customer;
import cn.wolfcode.domain.SysDictData;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.ICustomerService;
import cn.wolfcode.service.ISysDictDataService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;


/**
 * ClassName CustomerController
 * Create by Mike
 * Date 2021/8/1 16:52
 */
@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private ICustomerService customerService;

    @Autowired
    private ISysDictDataService sysDictDataService;

    @RequestMapping("/list")
    public String list(Model model, QueryObject qo){
        PageInfo<Customer> pageInfo = customerService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "/customer/list";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Customer customer){
        if (customer.getId() == null){
            customerService.save(customer);
        }else {
            customerService.update(customer);
        }
        return "redirect:/customer/list";
    }

    @RequestMapping("/input")
    public String input(Long id,Model model){

        List<SysDictData> sources = sysDictDataService.queryByType("source");
        model.addAttribute("sources",sources);

        List<SysDictData> schools = sysDictDataService.queryByType("school");
        model.addAttribute("schools",schools);

        if (id != null){
            model.addAttribute("customer",customerService.get(id));
        }
        return "customer/input";
    }

}
